# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

_realname=OpenBLAS
pkgbase=mingw-w64-openblas
pkgname=("${MINGW_PACKAGE_PREFIX}-openblas"
         $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-openblas64"))
pkgver=0.3.19
pkgrel=1
pkgdesc="An optimized BLAS library based on GotoBLAS2 1.13 BSD, providing optimized blas, lapack, and cblas (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.openblas.net/"
license=('BSD')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-openblas-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
             )
options=('!buildflags')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/xianyi/OpenBLAS/archive/v${pkgver}.tar.gz
        002-lgfortran-requires-lquadmath.patch)
install=${_realname}.install
sha256sums=('947f51bfe50c2a0749304fbe373e00e7637600b0a47b78a51382aeb30ca08562'
            'fecd847d029f5fa0d48c685e48e1d5efd413ead14452349fe78a66717fbce3e7')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  apply_patch_with_msg \
    002-lgfortran-requires-lquadmath.patch
}

_build_openblas() {
  _idx_opt=$1

  declare -a _build_type
  if check_option "debug" "n"; then
    _build_type+=("Release")
  else
    _build_type+=("Debug")
  fi

  unset CFLAGS
  unset CXXFLAGS

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_STATIC_LIBS=ON \
    -DDYNAMIC_ARCH=ON \
    -DUSE_THREAD=ON \
    -DNUM_THREADS=64 \
    -DTARGET=CORE2 \
    ${_idx_opt} \
    ../${_realname}-${pkgver}

  cmake --build .
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}-32" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-32"
  mkdir -p "${srcdir}/build-${MSYSTEM}-32" && cd "${srcdir}/build-${MSYSTEM}-32"

  msg2 "Build OpenBLAS with 32-bit indexing"
  _build_openblas ""


  if [ "${CARCH}" != "i686" ]; then
    [[ -d "${srcdir}/build-${MSYSTEM}-64" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-64"
    mkdir -p "${srcdir}/build-${MSYSTEM}-64" && cd "${srcdir}/build-${MSYSTEM}-64"

    msg2 "Build OpenBLAS with 64-bit indexing"
    _build_openblas "-DBINARY=64 -DINTERFACE64=1"
  fi
}

package_openblas() {
  pkgdesc="An optimized BLAS library based on GotoBLAS2 1.13 BSD, providing optimized blas, lapack, and cblas (mingw-w64)"

  cd "${srcdir}"/build-${MSYSTEM}-32

  DESTDIR=${pkgdir} cmake --install .

  # Install License
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/lapack-netlib/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-lapack
}

package_openblas64() {
  pkgdesc="An optimized BLAS library based on GotoBLAS2 1.13 BSD, providing optimized blas, lapack, and cblas with 64-bit indexing (mingw-w64)"
  provides=()
  conflicts=()
  replaces=()

  cd "${srcdir}"/build-${MSYSTEM}-64

  DESTDIR=${pkgdir} cmake --install .

  # Install License
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}64/LICENSE
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/lapack-netlib/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}64/LICENSE-lapack
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
